﻿#import os
#import sys




# Настройка вывода ошибок в файл

# Определяется текущая директория

#__dir__ = os.path.dirname(os.path.abspath(__file__))
#path_error = os.path.join(__dir__, "so_test_error.log")

# Вывод ошибок перенаправляется в файл error.log в текущей директории

#sys.stderr = open(path_error, 'a')


import postgresql.driver as pg_driver
import cymysql
import fdb
import pypyodbc
import cx_Oracle


class postgre:
	"This class connects and disconnects postgre db"
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self):
		self.driver=pg_driver
		self.user='palo'
		self.passwd='palo'
		self.host='sa-serv8'
		self.port=5432
		self.charset=None
		self.db='test_so'
		self.sid=None
		self.dsn=None
		self.state=False
		self.log=None
		self.conn=None
		self.cursor=None

	def connect(self):
		"This function opens connection to db"
		#print (self.connect.__doc__)
		#beginning of function suite
		if self.state==False:
			self.conn = self.driver.connect(user=self.user, password=self.passwd, host=self.host, port=self.port, database=self.db)
			self.state=True
			self.log='Connected'
		else:
			self.log='Already connected'
		#end of function suite
		return 
	
	def disconnect(self):
		"This function closes connection to db"
		#print (self.disconnect.__doc__)
		#end of function suite
		if self.state==True:
			self.conn.close()
			self.conn=None
			self.state=False
			self.log='Disconnected'
		else:
			self.log='Already disconnected'
		#end of function suite
		return

	#end of class suite

class mysql:
	"This class connects and disconnects mysql db"
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self):
		self.driver=cymysql
		self.user='palo'
		self.passwd='palo'
		self.host='sa-serv10.tb.local'
		self.port=None
		self.charset='utf8'
		self.db='palo'
		self.sid=None
		self.dsn=None
		self.state=False
		self.log=None
		self.conn=None
		self.cursor=None

	def connect(self):
		"This function opens connection to db"
		#print (self.connect.__doc__)
		#beginning of function suite
		if self.state==False:
			self.conn = self.driver.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset)
			self.cursor=self.conn.cursor()
			self.state=True
			self.log='Connected'
		else:
			self.log='Already connected'
		#end of function suite
		return 
	
	def disconnect(self):
		"This function closes connection to db"
		#print (self.disconnect.__doc__)
		#beginning of function suite
		if self.state==True:
			self.conn.close()
			self.conn=None
			self.cursor=None
			self.state=False
			self.log='Disconnected'
		else:
			self.log='Already disconnected'
		#end of function suite
		return

	#end of class suite

class firebird:
	"This class connects and disconnects firebird db"
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self):
		self.driver=fdb
		self.user='sysdba'
		self.passwd='imech'
		self.host='sa-fs7.tb.local'
		self.port=None
		self.charset=None
		self.db='C:\IM_SW\Search\DB\SEARCH4.GDB'
		self.sid=None
		self.dsn=None
		self.state=False
		self.log=None
		self.conn=None
		self.cursor=None
	
	def connect(self):
		"This function opens connection to db"
		#print (self.connect.__doc__)
		#beginning of function suite
		if self.state==False:
			self.conn = self.driver.connect(host=self.host, database=self.db, user=self.user, password=self.passwd)
			self.cursor = self.conn.cursor()
			self.state=True
			self.log='Connected'
		else:
			self.log='Already connected'
		#end of function suite
		return
	
	def disconnect(self):
		"This function closes connection to db"
		#print (self.disconnect.__doc__)
		#end of function suite
		if self.state==True:
			self.conn.close()
			self.conn=None
			self.cursor=None
			self.state=False
			self.log='Disconnected'
		else:
			self.log='Already disconnected'
		#end of function suite
		return

	#end of class suite
	
class odbc:
	"This class connects and disconnects odbc db"
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self):
		self.driver=pypyodbc
		self.user=None
		self.passwd=None
		self.host=None
		self.port=None
		self.charset=None
		self.db=None
		self.dsn='1sbdb05'
		self.state=False
		self.log=None
		self.conn=None
		self.cursor=None
	
	def connect(self):
		"This function opens connection to db"
		#print (self.connect.__doc__)
		#beginning of function suite
		if self.state==False:
			self.conn = self.driver.connect(dsn=self.dsn)
			self.cursor = self.conn.cursor()
			self.state=True
			self.log='Connected'
		else:
			self.log='Already connected'
		#end of function suite
		return
	
	def disconnect(self):
		"This function closes connection to db"
		#print (self.disconnect.__doc__)
		#end of function suite
		if self.state==True:
			self.conn.close()
			self.conn=None
			self.cursor=None
			self.state=False
			self.log='Disconnected'
		else:
			self.log='Already disconnected'
		#end of function suite
		return


	#end of class suite

class oracle:
	"This class connects and disconnects oracle db"
	#print (self.__doc__)
	#beginning of class suite
	def __init__(self):
		self.driver=cx_Oracle
		self.user='omp_adm'
		self.passwd='ivushka2011'
		self.host='sa-fs7.tb.local'
		self.port='1521'
		self.charset=None
		self.db=None
		self.sid='omegafs7'
		self.dsn=self.driver.makedsn(self.host, self.port, self.sid)
		self.state=False
		self.log=None
		self.conn=None
		self.cursor=None
	

	def connect(self):
		"This function opens connection to db"
		#print (self.connect.__doc__)
		#beginning of function suite
		if self.state==False:
			self.conn = self.driver.connect(self.user, self.passwd, self.dsn)
			self.cursor=self.conn.cursor()
			self.state=True
			self.log='Connected'
		else:
			self.log='Already connected'
		#end of function suite
		return 
	
	def disconnect(self):
		"This function closes connection to db"
		#print (self.disconnect.__doc__)
		#end of function suite
		if self.state==True:
			self.conn.close()
			self.conn=None
			self.cursor=None
			self.state=False
			self.log='Disconnected'
		else:
			self.log='Already disconnected'
		#end of function suite
		return

	#end of class suite


#Вывод ошибок возвращается "в исходное положение"

#sys.stderr.close()
#sys.stderr = sys.__stderr__